package com.example.libretranslatemobile;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.libretranslatemobile.settings.SettingsActivity;
import com.google.android.material.textfield.TextInputLayout;

import org.w3c.dom.Text;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    int SOURCE_FIELD_ADAPTER_POSITION = 0;
    int TARGET_FIELD_ADAPTER_POSITION = 1;
    Button buttonReplaceLanguage;
    LinearLayoutManager layoutManager;
    RecyclerView fieldList;
    FieldsAdapter fieldsAdapter;
    Handler handler;
    EditText sourceField, targetField;
    Spinner sourceSpinner, targetSpinner;
    TranslateProcess translateProcess;
    //TranslateProcess translateProcess = new TranslateProcess("https://libretranslate.com", "6527");

    Map<String, String> translationLanguages;
    String[] lang = {/*"Auto Detect (Experimental)",*/"English", "Russian", "Arabic", "Chinese", "French", "German", "Hindi", "Irish", "Italian", "Japanese", "Korean", "Portuguese", "Spanish"};

    public void init(){
        ///////////////////////////////////////////////////////////////////////// <trash code> will be replace
        translationLanguages = new HashMap<String, String>();
        //translationLanguages.put("Auto Detect (Experimental)", "auto");
        translationLanguages.put("English", "en");
        translationLanguages.put("Russian", "ru");
        translationLanguages.put("Arabic", "ar");
        translationLanguages.put("Chinese", "zh");
        translationLanguages.put("French", "fr");
        translationLanguages.put("German", "de");
        translationLanguages.put("Hindi", "hi");
        translationLanguages.put("Irish", "ga");
        translationLanguages.put("Italian", "it");
        translationLanguages.put("Japanese", "ja");
        translationLanguages.put("Korean", "ko");
        translationLanguages.put("Portuguese", "pt");
        translationLanguages.put("Spanish", "es");
        ///////////////////////////////////////////////////////////////////////// </trash code>
        fieldList = findViewById(R.id.recyclerView);
        layoutManager = new LinearLayoutManager(this);
        fieldsAdapter = new FieldsAdapter(2);

        sourceSpinner = createSpinner(this, R.id.spinner_source, lang, translationLanguages, SOURCE_FIELD_ADAPTER_POSITION);
        targetSpinner = createSpinner(this, R.id.spinner_target, lang, translationLanguages, TARGET_FIELD_ADAPTER_POSITION);

        buttonReplaceLanguage = findViewById(R.id.button_replace_language);
    }
    public void recyclerViewActs(){
        fieldList.setLayoutManager(layoutManager);
        fieldList.setHasFixedSize(true);
        fieldList.setAdapter(fieldsAdapter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupFab(findViewById(R.id.main_fab));
        init();
        recyclerViewActs();
        buttonReplaceLanguage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changePlaceDataSpinner(sourceSpinner, targetSpinner);
            }
        });


        ButterKnife.bind(this);
    }


    void setHandleMessage(EditText field){
        handler = new Handler(){
            public void handleMessage(Message msg){
                field.setText(msg.obj.toString());
            }
        };
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id == R.id.preferences){
            startActivity(new Intent(this, SettingsActivity.class));

        }
        return super.onOptionsItemSelected(item);
    }

    private void setupFab(View fab){
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                translateProcess = new TranslateProcess("https://libretranslate.de");
                sourceField = fieldList.findViewHolderForAdapterPosition(SOURCE_FIELD_ADAPTER_POSITION).itemView.findViewById(R.id.rv_field);
                targetField = fieldList.findViewHolderForAdapterPosition(TARGET_FIELD_ADAPTER_POSITION).itemView.findViewById(R.id.rv_field);
                targetField.setText("Loading wait please... -_-");
                setHandleMessage(targetField);
                translateProcess.translateStart(handler, translationLanguages.get(sourceSpinner.getSelectedItem().toString()), translationLanguages.get(targetSpinner.getSelectedItem().toString()), sourceField.getText().toString());
            }
        });
    }


    public Spinner createSpinner(Context context, int id, String[] data){
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, data);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Spinner spinner = (Spinner) findViewById(id);
        spinner.setAdapter(adapter);
        spinner.setSelection(0);
        return spinner;
    }

    public Spinner createSpinner(Context context, int id, String[] data, Map<String, String> translationLanguages, int fieldAdapterPosition){
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, data);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Spinner spinner = (Spinner) findViewById(id);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                setHintTextInputLayout(fieldAdapterPosition, translationLanguages.get(data[position]));
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        return spinner;
    }

    public void setHintTextInputLayout(int fieldAdapterPosition, String hintText){
         TextInputLayout textInputLayout = fieldList.findViewHolderForAdapterPosition(fieldAdapterPosition).itemView.findViewById(R.id.rv_text_input_layout);
         textInputLayout.setHint(hintText);
    }

    public void changePlaceDataSpinner(Spinner sourceSpinner, Spinner targetSpinner){
        int buffer = sourceSpinner.getSelectedItemPosition();
        sourceSpinner.setSelection(targetSpinner.getSelectedItemPosition());

        targetSpinner.setSelection(buffer);
    }
}